//
//  LoginViewController.m
//  test
//
//  Created by 朱圳 on 2017/12/21.
//  Copyright © 2017年 朱圳. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "ZTabBarController.h"
#import "JWZHMainTabar.h"
#import "ZHMainTabar.h"
#import "UserModel.h"
#import "UserManager.h"
#import "GRUserModel.h"//研究生
#import "GRUserManager.h"//研究生个人信息管理类
#import "JWUserModel.h"//教务端model
#import "JWUserManager.h"//教务端管理
#import "AddInfoViewController.h"
@interface LoginViewController ()<LoginViewDelegate>
@property(nonatomic,strong)LoginView *loginView;
@property(nonatomic,strong)UserModel *SDuser;
@property (nonatomic, strong) JWUserModel *JWmodel;
@property(nonatomic,strong)NSString *loginType;
@property(nonatomic,strong)NSDictionary *dict;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.loginView];
    //去黑线
    self.navigationController.navigationBar.clipsToBounds = YES;
    // 设置初始导航栏透明度
    [self wr_setNavBarBackgroundAlpha:0];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(LoginView *)loginView{
    
    if (!_loginView) {
        _loginView = [[LoginView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        _loginView.delegate = self;
    }
    return _loginView;
}

- (void)forgetButton:(UIButton *)sender {
    
}

- (void)loginButton:(UIButton *)sender withTel:(NSString *)tel andPassword:(NSString *)password {
    
    [[NSUserDefaults standardUserDefaults] setObject:tel forKey:@"rememberTel"];
    [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"rememberpassword"];
    [self.view endEditing:YES];
    BOOL isTel = [self validateMobile:tel];
    if (isTel == YES &&password.length>=3) {
        self.dict = @{
                      @"phone":tel,
                      @"password":password,
                      };
        [self StudentLogin];
        
    }else{
        [Utility showFail:@"电话格式不对或者密码小于3位"];
    }
    
}
/********登录*******/
-(void)StudentLogin{

    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    [[AFNetworkTool shareManager]postJSONWithUrl:Login parmas: self.dict successData:^(id json) {
        if ([json[@"messagetype"] isEqual:@"1"]) {
            //classification 为1学生端 2研究生端 3教务端
            if ([json[@"classification"] isEqualToString:@"1"]) {
                self.SDuser = [UserModel yy_modelWithJSON:json];
                [UserManager save:self.SDuser];
                //判断是否第一次登录，是则进入完善信息0第一次登录
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [EasyShowLodingView hidenLoding];
                    if ([json[@"revise"] isEqual:@"0"]) {
                        AddInfoViewController *vc = [[AddInfoViewController alloc]init];
                        [self presentViewController:vc animated:YES completion:nil];
                    }else{
                        
                        ZTabBarController * tabbar = [[ZTabBarController alloc]init];
                        [[UIApplication sharedApplication].delegate.window setRootViewController:tabbar];
                    }
                });
            }else if ([json[@"classification"] isEqualToString:@"2"]){
                
                GRUserModel *model =[GRUserModel yy_modelWithJSON:json];
                [GRUserManager save:model];
                ZHMainTabar *tabBarControllerConfig = [[ZHMainTabar alloc] init];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication].delegate.window setRootViewController:tabBarControllerConfig];
                    [EasyShowLodingView hidenLoding];
                });
                
            }else if ([json[@"classification"] isEqualToString:@"3"]){
                
                self.JWmodel = [JWUserModel yy_modelWithJSON:json];
                [JWUserManager save:self.JWmodel];
                
                JWZHMainTabar *tabBarControllerConfig = [[JWZHMainTabar alloc] init];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication].delegate.window setRootViewController:tabBarControllerConfig];
                    [EasyShowLodingView hidenLoding];
                });
                
            }else{
                NSLog(@"mei");
                [EasyShowLodingView hidenLoding];
            }
            
        }else{
            [Utility showFail:@"账号密码错误"];
            [EasyShowLodingView hidenLoding];
        }
    } failure:^(NSError *error) {
        [Utility showFail:@"网络访问异常"];
        [EasyShowLodingView hidenLoding];
    }];
}
- (void)registButton:(UIButton *)sender {
    
}

- (BOOL)validateMobile:(NSString *)mobile
{
    // 手机号以13、15、18、17开头，八个 \d 数字字符
    NSString * phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[0,0-9]))\\d{8}$";
    NSPredicate * phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

@end
